//
// Created by 11510 on 2024/6/22.
//
#include <iostream>
#include <memory>

using std::cout;
using std::endl;
using std::shared_ptr;
using std::unique_ptr;

class Point : public std::enable_shared_from_this<Point> {
public:
    Point(int x, int y) : _ix(x), _iy(y) {}


    //(1)
    /* Point * addPoint(Point * pt){ */
    /*     _ix += pt->_ix; */
    /*     _iy += pt->_iy; */
    /*     return this; */
    /* } */


    //(2)
//    shared_ptr<Point> addPoint(Point *pt) {
//        _ix += pt->_ix;
//        _iy += pt->_iy;
//        //创建的匿名的shared_ptr和sp共用一个裸指针
//        return shared_ptr<Point>(this);
//    }


    //(3)
    shared_ptr<Point> addPoint(Point * pt){
        _ix += pt->_ix;
        _iy += pt->_iy;
        return shared_from_this();
    }


    void print() const {
        cout << "(" << _ix
             << "," << _iy
             << ")" << endl;
    }

private:
    int _ix;
    int _iy;
};

void test0() {
    shared_ptr<Point> sp(new Point(1, 2));
    shared_ptr<Point> sp2(new Point(3, 4));

    //(1)如果使用sp的裸指针创建sp3
    //那么就是sp3和sp共同管理一个Point,却没有通过shared_ptr的复制
    //
    //(3)不存在任何的非法的使用同一个裸指针创建多个shared_ptr的情况
    shared_ptr<Point> sp3(sp->addPoint(sp2.get()));
    cout << "sp3 = ";
    sp3->print();
}


int main(void) {
    test0();
    return 0;
}